package com.sscaipiao.android.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.sscaipiao.android.R;
import com.sscaipiao.android.common.Constants;
import com.sscaipiao.android.model.User;
import com.sscaipiao.android.utils.StringUtil;
import com.sscaipiao.android.utils.UserDataServiceHelper;

public class Regist2Activity extends Activity implements OnClickListener{
	
	private Button backButton;
	private Button bankCardButton;
	private Button finishButton;
	private Button changePasswordButton;

	private EditText  regist2_txt_real_name;
	private EditText  regist2_txt_id_number;
	private EditText  regist2_txt_email;
	
	private User user;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.regist2);
		
		Intent i = this.getIntent();
		if (!i.equals(null)) {
			Bundle b = i.getExtras();
			if (b != null) {
				if (b.containsKey(Constants.USER)) {
					user = (User) b.get(Constants.USER);
				}
			}
		}
		
		backButton =  (Button) findViewById(R.id.regist2_button_back);
		finishButton = (Button) findViewById(R.id.regist2_finish);
		bankCardButton = (Button) findViewById(R.id.regist2_button_bank_card);
		changePasswordButton = (Button) findViewById(R.id.regist2_change_password);
		
		
		if(StringUtil.isEmptyString(user.getBankFullName())) {
			String bankInfo = UserDataServiceHelper.getBankInfo(
					Regist2Activity.this, user);
			String formerBank = "";
			String formerProvince = "";
			String formerCity = "";
			String formerBankFullName = "";
			String formerBankNo = "";
	
			try {
				JSONObject formerbankJson = new JSONObject(
						bankInfo.split(Constants.STRING_SPLITE_COMPLEX)[2]);
				formerBank = formerbankJson.getString(Constants.BANK);
				formerProvince = formerbankJson.getString("province");
				formerCity = formerbankJson.getString("city");
				formerBankFullName = formerbankJson.getString("bankFullName");
				formerBankNo = formerbankJson.getString("bankno");
				user.setBankno(formerBankNo);
				user.setBankFullName(formerBankFullName);	
				user.setBank(formerBank);
				user.setProvince(formerProvince);
				user.setCity(formerCity);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(user != null && StringUtil.isNotEmptyString(user.getBankno()) ){
			
			bankCardButton.setText(getResources().getString(R.string.regist2_bank_card) +
					getResources().getString(R.string.regist2_already_bind)  );

		} else {
			bankCardButton.setText(getResources().getString(R.string.regist2_bank_card) +
					getResources().getString(R.string.regist2_not_bind)  );
		}

		
		backButton.setOnClickListener(this);
		finishButton.setOnClickListener(this);
		bankCardButton.setOnClickListener(this);
		changePasswordButton.setOnClickListener(this);
		
		regist2_txt_real_name = (EditText)  findViewById(R.id.edit_text_regist2_real_name);
		regist2_txt_id_number = (EditText)  findViewById(R.id.edit_text_regist2_id);
		regist2_txt_email = (EditText)  findViewById(R.id.edit_text_regist2_email);
		
		if(user != null && StringUtil.isNotEmptyString(user.getName()) ){
			regist2_txt_real_name.setText(user.getName()  );
			regist2_txt_real_name.setEnabled(false);
		} 
		if(user != null && StringUtil.isNotEmptyString(user.getIdCard()) ){
			regist2_txt_id_number.setText(user.getIdCard()  );
			regist2_txt_id_number.setEnabled(false);
		} 
		if(user != null && StringUtil.isNotEmptyString(user.getEmail()) ){
			regist2_txt_email.setText(user.getEmail()  );
			//regist2_txt_email.setEnabled(false);
		} 		
	}


	@Override
	public void onClick(View v) {
		if(R.id.regist2_button_back == v.getId()) {

//			Intent intent = new Intent(Regist2Activity.this, RegistActivity.class);
//			Bundle b = new Bundle();
//			b.putSerializable(Constants.USER, user);
//			intent.putExtras(b);
//			startActivity(intent);
			
			Regist2Activity.this.finish();
			
		} else if (R.id.regist2_change_password == v.getId()){
			Intent intent = new Intent(Regist2Activity.this, ResetPasswordActivity.class);
			Bundle b = new Bundle();
			if (user == null) {
				user = new User();
			}
			
			b.putSerializable(Constants.USER, user);
			intent.putExtras(b);
			startActivity(intent);
		} else if (R.id.regist2_button_bank_card == v.getId()){
			
			Intent intent = new Intent(Regist2Activity.this, RegistBankBindActivity.class);
			Bundle b = new Bundle();
			b.putSerializable(Constants.USER, user);
			intent.putExtras(b);
			startActivity(intent);
		} else if (R.id.regist2_finish == v.getId()){
			if( StringUtil.isEmptyString(regist2_txt_real_name.getText().toString())){
				Toast.makeText(Regist2Activity.this, "请填写真实姓名", Toast.LENGTH_SHORT).show();
				return;
			} else if( StringUtil.isEmptyString(regist2_txt_id_number.getText().toString())){
				Toast.makeText(Regist2Activity.this, "请填写身份证号", Toast.LENGTH_SHORT).show();
				return;
			}else if( StringUtil.isEmptyString(regist2_txt_email.getText().toString())){
				Toast.makeText(Regist2Activity.this, "请填写邮箱", Toast.LENGTH_SHORT).show();
				return;
			}
			user.setName(regist2_txt_real_name.getText().toString());
			user.setIdCard(regist2_txt_id_number.getText().toString());
			user.setEmail(regist2_txt_email.getText().toString());
			UserDataServiceHelper.saveUserInfo(Regist2Activity.this, user);
			
			Intent intent = new Intent( Regist2Activity.this,HallActivity.class);
			Bundle b = new Bundle();
			b.putSerializable(Constants.USER, user);
			intent.putExtras(b);
			startActivity(intent);
				
		}

	}
}
